﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="PromotionsProduct.aspx.cs" Inherits="Promotion_PromotionsFormula" %>

<%@ Register Src="~/UserControl/GridPager.ascx" TagName="GridPager" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script src="../JavaScript/validatebox.js" type="text/javascript"></script>
    <script type="text/javascript">
        var f_ddl_data = eval('(' + '<%= ddl_forms %>' + ')');
        var p_ddl_data = eval('(' + '<%= ddl_Promotion %>' + ')');
        var u_ddl_data = eval('({"Table1":[{"ID":"PC","NAME":"盒"},{"ID":"CS","NAME":"箱"}]})');

        var columns = [[{ field: "PSCODE", title: "促销产品编号", width: 100 },
        { field: "PSFNAME", title: "促销产品系列", width: 100 },
        { field: "PSNAME", title: "促销产品名称", width: 100 },
        { field: "SV", title: "促销条件", width: 60 },
        { field: "SU", title: "促销单位", width: 60, formatter: function (value, rec) {
            var v = "";
            $.each(u_ddl_data.Table1, function (i, n) { if (n.ID == value) { v = n.NAME; return false; } });
            return v;
        }
        },
        { field: "PPCODE", title: "赠送产品编号", width: 100 },
        { field: "PPFNAME", title: "赠送产品系列", width: 100 },
        { field: "PPNAME", title: "赠送产品名称", width: 100 },
        { field: "PV", title: "赠送条件", width: 60 },
        { field: "PU", title: "赠送单位", width: 60, formatter: function (value, rec) {
            var v = ""; $.each(u_ddl_data.Table1, function (i, n) { if (n.ID == value) { v = n.NAME; return false; } }); return v;
        }
        },
        { field: "HT", title: "促销类型", width: 100, formatter: function (value, rec) {
            var v = ""; $.each(p_ddl_data.Table1, function (i, n) { if (n.ID == value) { v = n.NAME; return false; } }); return v;
        }
        }]];

        function AjaxPost(method, obj, arr) {
            var dataArr = "{'d': '<%= MyStateManager.DomainID %>','l':'<%= MyStateManager.UserID %>'" + arr + "}";
            $.ajax({
                url: "PromotionsProduct.aspx/" + method,
                data: dataArr,
                type: "post",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (getdata) {
                    switch (obj) {
                        case "Save":
                            Saveed(getdata);
                            break;
                    }
                },
                error: function (xhr) {
                    $.messager.alert('警告消息', xhr.responseText, 'warning');
                }
            });
        }

        var qParams = { p: '<%= Promotion_id %>', mode: 'Qry' };
        function load() {
            $('#table1').datagrid({
                minWidth: 500,
                height: 350,
                rownumbers: true,
                columns: columns,
                singleSelect: true,
                pagination: true,
                queryParams: qParams,
                url: 'PP_Handler.ashx',
                loadMsg: '正在加载,请稍候...',
                toolbar: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () { reset(); open1(); }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        if ($('#table1').datagrid('getSelected') != null) { update($('#table1').datagrid('getSelected')); open1(); }
                        else { $.messager.alert('警告消息', '请选择一行数据.', 'warning'); }
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        if ($('#table1').datagrid('getSelected') != null) {
                            $.messager.defaults.ok = '确认';
                            $.messager.defaults.cancel = '取消';
                            $.messager.confirm('提示消息', '确认删除数据?', function (b) { if (b) { Save(2); } });
                        }
                        else { $.messager.alert('警告消息', '请选择一行数据.', 'warning'); }
                    }
                }],
                onSelect: function (i, r) {
                    $('#hidden1').val(r.ID);
                }
            });
            $('#divS').window({
                title: '促销活动维护',
                minimizable: false,
                maximizable: false,
                collapsible: false,
                resizable: false,
                modal: true,
                top: ($(window).height() - 400) / 2
            });
            close1();
            Query();
            comboEvent();
        }

        function Query() {
            $('#table1').datagrid('options').queryParams = qParams;
            var p = $('#table1').datagrid('getPager');
            if (p) {
                $(p).pagination({ pageNumber: $('#table1').datagrid('options').pageNumber, beforePageText: '第', //页数文本框前显示的汉字  
                    afterPageText: '页    共 {pages} 页',
                    displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
                });
            }
            $("#table1").datagrid('reload');
            return false;
        }
        function Saveed(getdata) {
            if (getdata.d == "1") {
                $.messager.alert('提示消息', '提交数据成功!', 'info');
                close1(); Query();
            }
            else if (getdata.d == "") {
                $.messager.alert('提示消息', '活动代码存在!', 'info');
            }
        }
        function Save(i) {
            var arr = "";
            switch (i) {
                case 0:
                    arr = ",'i':-1,'pr':'<%= Promotion_id %>','ps':'" + $('#select3').combobox('getValue') + "','sv':'" + $('#text1').val() + "','su':'" + $('#select4').combobox('getValue') + "','pp':'" + $('#select6').combobox('getValue') + "','pv':'" + $('#text2').val() + "','pu':'" + $('#select7').combobox('getValue') + "','ht':'" + $('#select1').combobox('getValue') + "'";
                    break;
                case 1:
                    arr = ",'i':" + $('#hidden1').val() + ",'pr':'<%= Promotion_id %>','ps':'" + $('#select3').combobox('getValue') + "','sv':'" + $('#text1').val() + "','su':'" + $('#select4').combobox('getValue') + "','pp':'" + $('#select6').combobox('getValue') + "','pv':'" + $('#text2').val() + "','pu':'" + $('#select7').combobox('getValue') + "','ht':'" + $('#select1').combobox('getValue') + "'";
                    break;
                case 2:
                    arr = ",'i':" + $('#table1').datagrid('getSelected').ID + ",'pr':'-1','ps':'-1','sv':'-1','su':'','pp':'-1','pv':'-1','pu':'','ht':'-1'";
                    break;
            }
            AjaxPost('Svae', 'Save', arr);
        }
        function update(data) {
            $('#select1').combobox('setValue', data.HT);
            $('#select1').combobox('loadData', p_ddl_data.Table1);

            $('#select2').combobox('setValue', data.PSFCODE);
            $('#select2').combobox('loadData', f_ddl_data.Table);
            bindCombo($('#select3'), data.PSFCODE, data.PSID);
            $('#select4').combobox('setValue', data.SU);
            $('#select4').combobox('loadData', u_ddl_data.Table1);

            $('#select5').combobox('setValue', data.PPFCODE);
            $('#select5').combobox('loadData', f_ddl_data.Table);
            bindCombo($('#select6'), data.PPFCODE, data.PPID);
            $('#select7').combobox('setValue', data.PU);
            $('#select7').combobox('loadData', u_ddl_data.Table1);

            $('#select5').combobox('disable');
            $('#select6').combobox('disable');

            $('#text1').val(data.SV); $('#text2').val(data.PV); $('#hidden1').val(data.ID);
        }
        function reset() {
            $('#select1').combobox('setValue', '1');
            $('#select1').combobox('loadData', p_ddl_data.Table1);

            $('#select2').combobox('setValue', f_ddl_data.Table[0].ID);
            $('#select2').combobox('loadData', f_ddl_data.Table);
            bindCombo($('#select3'), f_ddl_data.Table[0].ID);
            $('#select4').combobox('setValue', 'PC');
            $('#select4').combobox('loadData', u_ddl_data.Table1);

            $('#select5').combobox('setValue', f_ddl_data.Table[0].ID);
            $('#select5').combobox('loadData', f_ddl_data.Table);
            bindCombo($('#select6'), f_ddl_data.Table[0].ID);
            $('#select7').combobox('setValue', 'PC');
            $('#select7').combobox('loadData', u_ddl_data.Table1);

            $('#select5').combobox('disable');
            $('#select6').combobox('disable');

            $('#text1').val(""); $('#text2').val(""); $('#hidden1').val("");
        }
        function comboEvent() {
            $('#select1').combobox({
                onSelect: function (record) {
                    if (record.ID == "2") {
                        $('#select5').combobox('enable');
                        $('#select6').combobox('enable');
                    }
                    else {
                        $('#select5').combobox('disable');
                        $('#select6').combobox('disable');
                    }
                }
            });
            $('#select2').combobox({
                onSelect: function (record) {
                    bindCombo($('#select3'), record.ID);
                }
            });
            $('#select5').combobox({
                onSelect: function (record) {
                    bindCombo($('#select6'), record.ID);
                }
            });
        }
        function bindCombo(obj, id, sid) {
            $.ajax({ url: "PromotionsProduct.aspx/getFormsToProduct",
                data: "{ 'f': '" + id + "' }", type: "post",
                contentType: "application/json; charset=utf-8",
                dataType: "json", success: function (getdata) {
                    var data = eval('(' + getdata.d + ')');
                    if (sid)
                        obj.combobox('setValue', sid);
                    else
                        obj.combobox('setValue', data.Table[0].ID);
                    obj.combobox('loadData', data.Table);
                }, error: function (xhr) { $.messager.alert('警告消息', xhr.responseText, 'warning'); }
            });
        }
        function open1() {
            $('#divS').window('open');
            $('#divS').css({ display: 'block' });
        }
        function close1() {
            $('#divS').window('close');
        }
        $(function () {
            load();
            $('#btn_Save').click(function () {
                var b = true;
                $('#tab input').each(function () {
                    if ($(this).attr('required') || $(this).attr('validType'))
                        if (!$(this).validatebox('isValid')) { b = false; return false; }
                });
                if (b)
                    if ($('#hidden1').val() == "") Save(0); else Save(1);
            });
        });

    </script>
    <table id="wsd_inputtable" border="0">
        <tr>
            <td class="tabletitle" style="height: 21px" width="100%" colspan="9">
                促销活动内容
            </td>
        </tr>
        <tr>
            <td class="tablefield">
                活动代码:
            </td>
            <td>
                <asp:Label ID="Label1" runat="server">
                </asp:Label>
            </td>
            <td class="tablefield">
                活动名称:
            </td>
            <td>
                <asp:Label ID="Label2" runat="server">
                </asp:Label>
            </td>
            <td class="tablefield">
                开始时间:
            </td>
            <td>
                <asp:Label ID="Label3" runat="server">
                </asp:Label>
            </td>
            <td class="tablefield">
                结束时间:
            </td>
            <td>
                <asp:Label ID="Label4" runat="server">
                </asp:Label>
            </td>
            <td style="width: 100px;">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="tablefield">
                状态:
            </td>
            <td>
                <asp:Label ID="Label8" runat="server">
                </asp:Label>
            </td>
            <td class="tablefield">
                活动描述:
            </td>
            <td colspan="5">
                <asp:Label ID="Label9" runat="server">
                </asp:Label>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
    <br />
    <table id="table1">
    </table>
    <br />
    <table id="wsd_inputtable">
        <tr>
            <td width="100%" class="tabletitle">
                <asp:Literal ID="Literal40" runat="server" Text="<%$resources:language,lbl_Operation_Area %>"></asp:Literal>
            </td>
        </tr>
        <tr>
            <td class="buttonarea">
                <input id="Button2" class="wsd_button2" runat="server" onclick="location.href='Promotions.aspx'"
                    type="button" value="<%$resources:language,btn_Cancel %>" />
            </td>
        </tr>
    </table>
    <div id="divS" class="easyui-window" icon="icon-save" style="width: 350px; height: 400px;
        padding: 5px; background: #fafafa; display: none;">
        <div class="easyui-layout" fit="true">
            <div region="center" border="false" style="padding: 10px; background: #fff; border: 1px solid #ccc;">
                <table style="margin: 0 0 0 0; border: 0; width: 100%; height: 300px;" id="tab">
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                促销类型:</label>
                            <select id="select1" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false" required="true" invalidmessage="此项为必选项!">
                            </select>
                            <span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                促销产品系列:</label>
                            <select id="select2" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false" required="true" missingmessage="此项为必选项!">
                            </select><span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                促销产品名称:</label>
                            <select id="select3" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false" required="true" missingmessage="此项为必选项!">
                            </select><span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                促销条件:</label>
                            <input class="easyui-validatebox" type="text" id="text1" required="true" missingmessage="此项为必填项!"
                                style="width: 150px;" validtype="number" /><span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                促销产品单位:</label>
                            <select id="select4" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false">
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                赠送产品系列:</label>
                            <select id="select5" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false">
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                赠送产品名称:</label>
                            <select id="select6" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false">
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                赠送条件:</label>
                            <input class="easyui-validatebox" type="text" id="text2" required="true" missingmessage="此项为必填项!"
                                style="width: 150px;" validtype="number" /><span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 100px; padding-left: 10px;">
                                赠送产品单位:</label>
                            <select id="select7" style="width: 150px;" class="easyui-combobox" valuefield="ID"
                                textfield="NAME" editable="false">
                            </select>
                        </td>
                    </tr>
                </table>
            </div>
            <div region="south" border="false" style="text-align: right; height: 30px; line-height: 30px;
                padding-top: 5px;">
                <input type="hidden" id="hidden1" />
                <a class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" id="btn_Save">
                    提交</a> <a class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)"
                        onclick="close1()">取消</a>
            </div>
        </div>
    </div>
</asp:Content>
